<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('product_compares', function (Blueprint $table) {
            $table->id();
            $table->foreignId('user_id')->nullable()->constrained()->onDelete('cascade')->comment('用户ID（登录用户）');
            $table->string('session_id')->nullable()->comment('会话ID（游客）');
            $table->foreignId('product_id')->constrained()->onDelete('cascade')->comment('产品ID');
            $table->timestamps();
            
            // 确保同一用户/会话不能重复添加同一产品
            $table->unique(['user_id', 'product_id']);
            $table->unique(['session_id', 'product_id']);
            
            $table->index('user_id');
            $table->index('session_id');
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('product_compares');
    }
};
